package com.dbq.share;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import static com.dbq.share.SocializeMediaConstant.FACEBOOK;
import static com.dbq.share.SocializeMediaConstant.QQ;
import static com.dbq.share.SocializeMediaConstant.QZONE;
import static com.dbq.share.SocializeMediaConstant.SINA;
import static com.dbq.share.SocializeMediaConstant.WEIXIN;
import static com.dbq.share.SocializeMediaConstant.WEIXIN_TIMELINE;

/**
 * Created by sunkai on 2017/12/26.
 */

public class YibaShareDelegateActivity extends Activity {
    static final String KEY_SHARE_TARGET = "share_target";
    static final String KEY_SHARE_WEBPAGE = "share_webpage";
    static final String KEY_IS_QZONE = "isQZone";
    static final String KEY_IS_TIMELINE = "isTimeline";
    static final String KEY_UUID = "shareUtiluid";

    public static void start(Context context, ShareTarget item, ShareWebPage shareWebPage, String uuid) {
        Intent intent = new Intent(context, YibaShareDelegateActivity.class);
        intent.putExtra(KEY_SHARE_TARGET, item);
        intent.putExtra(KEY_SHARE_WEBPAGE, shareWebPage);
        intent.putExtra(KEY_UUID, uuid);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private ShareTarget shareTarget;
    private ShareWebPage shareWebPage;
    private String uuid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().getExtras() == null || !getIntent().getExtras().containsKey(KEY_SHARE_TARGET) || !getIntent().getExtras().containsKey(KEY_SHARE_WEBPAGE)) {
            //TODO
            Log.e("SK", "param is null");
            finish();
            return;
        }

        shareTarget = getIntent().getExtras().getParcelable(KEY_SHARE_TARGET);
        shareWebPage = getIntent().getExtras().getParcelable(KEY_SHARE_WEBPAGE);
        uuid = getIntent().getExtras().getString(KEY_UUID, "");
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (shareTarget.media) {
            case SINA:
                shareToSina();
                break;
            case WEIXIN:
                shareToWX(false);
                break;
            case WEIXIN_TIMELINE:
                shareToWX(true);
                break;
            case QQ:
                shareToQQ(false);
                break;
            case QZONE:
                shareToQQ(true);
                break;
            case FACEBOOK:
                shareToFacebook();
                break;
        }
        finish();
    }

    private void shareToFacebook() {
        Intent intent = new Intent(this, FacebookAssistActivity.class);
        intent.putExtra(KEY_SHARE_TARGET, shareTarget);
        intent.putExtra(KEY_SHARE_WEBPAGE, shareWebPage);
        intent.putExtra(KEY_UUID, uuid);
        startActivity(intent);

    }

    private void shareToQQ(boolean isQZone) {
        Intent intent = new Intent(this, QQAssistActivity.class);
        intent.putExtra(KEY_SHARE_TARGET, shareTarget);
        intent.putExtra(KEY_SHARE_WEBPAGE, shareWebPage);
        intent.putExtra(KEY_UUID, uuid);
        intent.putExtra(KEY_IS_QZONE, isQZone);
        startActivity(intent);
    }

    private void shareToWX(boolean isTimeLine) {
        Intent intent = new Intent(this, WxAssistActivity.class);
        intent.putExtra(KEY_SHARE_TARGET, shareTarget);
        intent.putExtra(KEY_SHARE_WEBPAGE, shareWebPage);
        intent.putExtra(KEY_UUID, uuid);
        intent.putExtra(KEY_IS_TIMELINE, isTimeLine);
        startActivity(intent);
    }

    private void shareToSina() {
        Intent intent = new Intent(this, SinaAssistActivity.class);
        intent.putExtra(KEY_SHARE_TARGET, shareTarget);
        intent.putExtra(KEY_SHARE_WEBPAGE, shareWebPage);
        intent.putExtra(KEY_UUID, uuid);
        startActivity(intent);
    }
}
